#!/bin/bash

# 清屏并显示信息
clear
echo -e "小战云原创脚本官网：https://idc.sanxiaozhan.org/"
echo -e "小战云 为您提供稳定可靠的云服务 国内服务器40元起，海外服务器20元起"
sleep 5s

# 定义颜色
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 检测Docker是否安装
if command -v docker &> /dev/null
then
	    echo -e "${GREEN}Docker 已安装${NC}"
    else
	        echo -e "${RED}请先安装Docker${NC}"
		    exit 1
fi

# 询问是否更新Docker和Docker Compose
read -p "是否更新Docker和Docker Compose到最新版? (y/n): " update_docker
if [ "$update_docker" = "y" ]; then
	    echo "正在检测操作系统类型..."
	        
	        # 检测操作系统类型并更新Docker和Docker Compose
		    if [ -f /etc/os-release ]; then
			            . /etc/os-release
				            OS=$ID
					            VER=$VERSION_ID
						        else
								        echo -e "${RED}无法检测操作系统类型${NC}"
									        exit 1
										    fi

										        case "$OS" in
												        ubuntu|debian)
														            echo "正在更新Docker和Docker Compose到最新版 (Ubuntu/Debian)..."
															                sudo apt-get update
																	            sudo apt-get install -y docker-ce docker-ce-cli containerd.io
																		                sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
																				            sudo chmod +x /usr/local/bin/docker-compose
																					                ;;
																							        centos)
																									            echo "正在更新Docker和Docker Compose到最新版 (CentOS)..."
																										                sudo yum install -y yum-utils
																												            sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
																													                sudo yum install -y docker-ce docker-ce-cli containerd.io
																															            sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
																																                sudo chmod +x /usr/local/bin/docker-compose
																																		            ;;
																																			            fedora)
																																					                echo "正在更新Docker和Docker Compose到最新版 (Fedora)..."
																																							            sudo dnf -y install dnf-plugins-core
																																								                sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
																																										            sudo dnf install -y docker-ce docker-ce-cli containerd.io
																																											                sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
																																													            sudo chmod +x /usr/local/bin/docker-compose
																																														                ;;
																																																        *)
																																																		            echo -e "${RED}不支持的操作系统: $OS${NC}"
																																																			                exit 1
																																																					            ;;
																																																						        esac

																																																							    if [ $? -eq 0 ]; then
																																																								            echo -e "${GREEN}Docker 和 Docker Compose 已更新到最新版${NC}"
																																																									        else
																																																											        echo -e "${RED}Docker 和 Docker Compose 更新失败${NC}"
																																																												        exit 1
																																																													    fi
																																																												    elif [ "$update_docker" = "n" ]; then
																																																													        echo "跳过Docker和Docker Compose更新"
																																																													else
																																																														    echo -e "${RED}无效的输入，退出脚本${NC}"
																																																														        exit 1
fi

# 检测Nginx是否安装
if command -v nginx &> /dev/null
then
	    echo -e "${GREEN}Nginx 已安装${NC}"
    else
	        echo -e "${RED}请先安装Nginx${NC}"
		    exit 1
fi

# 进入/root目录
cd /root

# 克隆仓库
git clone https://github.com/jamebal/jmal-cloud-server.git

# 进入克隆的目录
cd jmal-cloud-server

# 授予jc.sh脚本执行权限
chmod 777 jc.sh

# 执行jc.sh脚本的install命令
./jc.sh install
